package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.Manifest;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPicAdapter;
import flc.ast.bean.MyPicBean;
import flc.ast.databinding.ActivitySelPicBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    private SelPicAdapter picAdapter;
    private List<MyPicBean> listPic = new ArrayList<>();
    private String flag = "";
    private boolean isMoreSel = false;//是否多选
    private List<String> listSel = new ArrayList<>();
    private int oldPosition = 0;
    private String selPath="";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivSelPicBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvSelPicRight.setOnClickListener(this);
        mDataBinding.rvSelPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        picAdapter = new SelPicAdapter();
        mDataBinding.rvSelPicList.setAdapter(picAdapter);
        picAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSelPicRight:
                if (isMoreSel){
                    if (Content.Placard.equals(flag)){
                        if (listSel.size()!=3){
                            ToastUtils.showShort(getString(R.string.tip_placard));
                            return;
                        }
                        PlacardActivity.mPaths=listSel;
                        startActivity(PlacardActivity.class);
                    }else {
                        if (listSel.size() == 0) {
                            ToastUtils.showShort(getString(R.string.please_sel_pic));
                            return;
                        }
                        if (Content.Split_Zy_Zy.equals(flag)) {//自由拼接-左右
                            gotoPuzzle(4, getString(R.string.type_text2), false);
                        } else if (Content.Split_Zy_Fx.equals(flag)) {//自由拼接-方形
                            gotoPuzzle(0, getString(R.string.type_text1), false);
                        } else if (Content.Split_Zy_Sx.equals(flag)) {//自由拼接-上下
                            gotoPuzzle(2, getString(R.string.type_text3), false);
                        } else if (Content.Split_Shu.equals(flag)) {//竖图拼接
                            gotoOri(true);
                        } else if (Content.Split_Heng.equals(flag)) {//横图拼接
                            gotoOri(false);
                        } else if (Content.Split_Zy.equals(flag)) {//自由拼接
                            gotoPuzzle(0, getString(R.string.title_zy_split), true);
                        }
                    }
                }else {
                    if (selPath.isEmpty()){
                        ToastUtils.showShort(getString(R.string.please_sel_pic));
                        return;
                    }
                    if (Content.Edit_Filter.equals(flag)) {//滤镜
                        PicFilterActivity.imgPath=selPath;
                        startActivity(PicFilterActivity.class);
                    }else if (Content.Edit_Sticker.equals(flag)) {//贴纸
                        PicDecalActivity.imgPath=selPath;
                        startActivity(PicDecalActivity.class);
                    }else if (Content.Edit_Doodle.equals(flag)) {//涂鸦
                        PicDoodleActivity.imgPath=selPath;
                        startActivity(PicDoodleActivity.class);
                    }else if (Content.Edit_Cut.equals(flag)) {//裁剪
                        PicCutActivity.imgPath=selPath;
                        startActivity(PicCutActivity.class);
                    }else if (Content.Edit_Rotate.equals(flag)) {//旋转
                        PicRotateActivity.imgPath=selPath;
                        startActivity(PicRotateActivity.class);
                    }else if (Content.Edit_Mosaic.equals(flag)) {//马赛克
                        PicMosaicActivity.imgPath=selPath;
                        startActivity(PicMosaicActivity.class);
                    }
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isMoreSel){
            if (picAdapter.getItem(position).isSelect()) {
                picAdapter.getItem(position).setSelect(false);
                listSel.remove(picAdapter.getItem(position).getPath());
                picAdapter.notifyItemChanged(position);
            } else {
                if (Content.Split_Zy_Zy.equals(flag) || Content.Split_Zy_Sx.equals(flag)) {
                    if (listSel.size() == 3) {
                        return;
                    }
                } else if (Content.Split_Zy_Fx.equals(flag)) {
                    if (listSel.size() == 4) {
                        return;
                    }
                } else if (Content.Split_Shu.equals(flag) || Content.Split_Heng.equals(flag) || Content.Split_Zy.equals(flag)) {
                    if (listSel.size() == 9) {
                        return;
                    }
                }else if (Content.Placard.equals(flag)) {
                    if (listSel.size() == 3) {
                        return;
                    }
                }
                listSel.add(picAdapter.getItem(position).getPath());
                picAdapter.getItem(position).setSelect(true);
                picAdapter.notifyItemChanged(position);
            }
        }else {
            //单选
            picAdapter.getItem(oldPosition).setSelect(false);
            picAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            picAdapter.getItem(position).setSelect(true);
            picAdapter.notifyItemChanged(position);
            selPath=picAdapter.getItem(position).getPath();
        }
    }

    @Override
    protected void initData() {
        //获取标记
        flag = getIntent().getStringExtra(Content.Flag);
        if (Content.Split_Zy_Zy.equals(flag) || Content.Split_Zy_Sx.equals(flag)) {
            ToastUtils.showShort(getString(R.string.tip_zy));
            isMoreSel=true;
        } else if (Content.Split_Zy_Fx.equals(flag)) {
            ToastUtils.showShort(getString(R.string.tip_fx));
            isMoreSel=true;
        } else if (Content.Split_Shu.equals(flag) || Content.Split_Heng.equals(flag) || Content.Split_Zy.equals(flag)) {
            ToastUtils.showShort(getString(R.string.tip_shu));
            isMoreSel=true;
        }else if (Content.Placard.equals(flag)) {
            ToastUtils.showShort(getString(R.string.tip_placard));
            isMoreSel=true;
        }else {
            isMoreSel=false;
        }
        getPermission();
    }

    //去到拼图
    private void gotoPuzzle(int sel, String title, boolean tip) {
        PuzzleActivity.listPath = listSel;
        PuzzleActivity.defaultSel = sel;
        PuzzleActivity.title = title;
        startActivity(PuzzleActivity.class);
    }

    //去到横竖拼接
    private void gotoOri(boolean type) {
        OriActivity.type = type;
        OriActivity.listPath = listSel;
        startActivity(OriActivity.class);
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvSelPicList.setVisibility(View.GONE);
                        mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listPic.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    for (SelectMediaEntity entity : selectMediaEntities) {
                        listPic.add(new MyPicBean(entity.getPath(), false));
                    }
                    picAdapter.setList(listPic);
                    mDataBinding.rvSelPicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelPicList.setVisibility(View.GONE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}